import numpy as np


def colormap(rgb=False):
    color_list = np.array(
        [
            0.000, 0.447, 0.741,
            0.850, 0.325, 0.098,
            0.929, 0.694, 0.125,
            0.494, 0.184, 0.556,
            0.466, 0.674, 0.188,
            0.301, 0.745, 0.933,
            0.635, 0.078, 0.184,
            0.300, 0.300, 0.300,
            0.600, 0.600, 0.600,
            1.000, 0.000, 0.000,
            1.000, 0.500, 0.000,
            0.749, 0.749, 0.000,
            0.000, 1.000, 0.000,
            0.000, 0.000, 1.000,
            0.667, 0.000, 1.000,
            0.333, 0.333, 0.000,
            0.333, 0.667, 0.000,
            0.333, 1.000, 0.000,
            0.667, 0.333, 0.000,
            0.667, 0.667, 0.000,
            0.667, 1.000, 0.000,
            1.000, 0.333, 0.000,
            1.000, 0.667, 0.000,
            1.000, 1.000, 0.000,
            0.000, 0.333, 0.500,
            0.000, 0.667, 0.500,
            0.000, 1.000, 0.500,
            0.333, 0.000, 0.500,
            0.333, 0.333, 0.500,
            0.333, 0.667, 0.500,
            0.333, 1.000, 0.500,
            0.667, 0.000, 0.500,
            0.667, 0.333, 0.500,
            0.667, 0.667, 0.500,
            0.667, 1.000, 0.500,
            1.000, 0.000, 0.500,
            1.000, 0.333, 0.500,
            1.000, 0.667, 0.500,
            1.000, 1.000, 0.500,
            0.000, 0.333, 1.000,
            0.000, 0.667, 1.000,
            0.000, 1.000, 1.000,
            0.333, 0.000, 1.000,
            0.333, 0.333, 1.000,
            0.333, 0.667, 1.000,
            0.333, 1.000, 1.000,
            0.667, 0.000, 1.000,
            0.667, 0.333, 1.000,
            0.667, 0.667, 1.000,
            0.667, 1.000, 1.000,
            1.000, 0.000, 1.000,
            1.000, 0.333, 1.000,
            1.000, 0.667, 1.000,
            0.167, 0.000, 0.000,
            0.333, 0.000, 0.000,
            0.500, 0.000, 0.000,
            0.667, 0.000, 0.000,
            0.833, 0.000, 0.000,
            1.000, 0.000, 0.000,
            0.000, 0.167, 0.000,
            0.000, 0.333, 0.000,
            0.000, 0.500, 0.000,
            0.000, 0.667, 0.000,
            0.000, 0.833, 0.000,
            0.000, 1.000, 0.000,
            0.000, 0.000, 0.167,
            0.000, 0.000, 0.333,
            0.000, 0.000, 0.500,
            0.000, 0.000, 0.667,
            0.000, 0.000, 0.833,
            0.000, 0.000, 1.000,
            0.000, 0.000, 0.000,
            0.143, 0.143, 0.143,
            0.286, 0.286, 0.286,
            0.429, 0.429, 0.429,
            0.571, 0.571, 0.571,
            0.714, 0.714, 0.714,
            0.857, 0.857, 0.857,
            1.000, 1.000, 1.000
        ]
    ).astype(np.float32)
    color_list = color_list.reshape((-1, 3)) * 255
    if not rgb:
        color_list = color_list[:, ::-1]
    return color_list


# BGR for PSACAL VOC
VOC21 = {0: [0, 0, 0], 1: [0, 0, 128], 2: [0, 128, 0], 3: [0, 128, 128], 4: [128, 0, 0], 5: [128, 0, 128],
         6: [128, 128, 0], 7: [128, 128, 128], 8: [0, 0, 64], 9: [0, 0, 192], 10: [0, 128, 64],
         11: [0, 128, 192], 12: [128, 0, 64], 13: [128, 0, 192], 14: [128, 128, 64], 15: [128, 128, 192],
         16: [0, 64, 0], 17: [0, 64, 128], 18: [0, 192, 0], 19: [0, 192, 128], 20: [128, 64, 0],
         255: [255, 255, 255]}

# BGR for MSCOCO
COCO81 = {0: [0, 0, 0], 1: [28, 13, 251], 2: [27, 25, 251], 3: [28, 40, 252], 4: [29, 57, 252], 5: [31, 72, 251],
          6: [32, 95, 253], 7: [33, 112, 253], 8: [36, 130, 253], 9: [38, 147, 253], 10: [42, 170, 253],
          11: [44, 188, 253], 12: [48, 206, 254], 13: [50, 223, 255], 14: [54, 243, 252], 15: [56, 253, 245],
          16: [55, 254, 227], 17: [52, 253, 210], 18: [51, 253, 186], 19: [51, 253, 170], 20: [49, 253, 154],
          21: [50, 253, 136], 22: [48, 253, 114], 23: [48, 253, 100], 24: [48, 253, 84], 25: [47, 253, 70],
          26: [48, 253, 54], 27: [47, 253, 45], 28: [48, 253, 44], 29: [54, 253, 41], 30: [62, 253, 42],
          31: [78, 253, 42], 32: [92, 253, 43], 33: [107, 253, 41], 34: [122, 253, 42], 35: [143, 253, 41],
          36: [160, 253, 42], 37: [177, 254, 43], 38: [195, 254, 42], 39: [217, 254, 44], 40: [233, 254, 45],
          41: [251, 254, 45], 42: [253, 240, 42], 43: [252, 217, 37], 44: [252, 200, 34], 45: [251, 183, 30],
          46: [253, 165, 27], 47: [251, 143, 25], 48: [251, 126, 21], 49: [250, 109, 18], 50: [251, 93, 16],
          51: [251, 73, 14], 52: [251, 59, 12], 53: [251, 47, 12], 54: [251, 40, 13], 55: [252, 36, 17],
          56: [251, 36, 35], 57: [251, 36, 51], 58: [251, 36, 69], 59: [250, 36, 85], 60: [251, 37, 108],
          61: [251, 37, 126], 62: [251, 37, 143], 63: [250, 37, 160], 64: [251, 39, 183], 65: [252, 39, 202],
          66: [252, 39, 218], 67: [252, 40, 237], 68: [242, 39, 250], 69: [228, 36, 251], 70: [211, 33, 252],
          71: [194, 32, 252], 72: [171, 27, 253], 73: [154, 25, 252], 74: [136, 23, 251], 75: [120, 20, 252],
          76: [97, 18, 253], 77: [82, 16, 252], 78: [65, 16, 252], 79: [50, 14, 252], 80: [39, 13, 251],
          255: [255, 255, 255]}

# BGR for ADE20k
ADE151 = {0: [0, 0, 0], 1: [120, 120, 120], 2: [180, 120, 120], 3: [6, 230, 230], 4: [80, 50, 50], 5: [4, 200, 3],
          6: [120, 120, 80], 7: [140, 140, 140], 8: [204, 5, 255], 9: [230, 230, 230], 10: [4, 250, 7],
          11: [224, 5, 255], 12: [235, 255, 7], 13: [150, 5, 61], 14: [120, 120, 70], 15: [8, 255, 51],
          16: [255, 6, 82], 17: [143, 255, 140], 18: [204, 255, 4], 19: [255, 51, 7], 20: [204, 70, 3],
          21: [0, 102, 200], 22: [61, 230, 250], 23: [255, 6, 51], 24: [11, 102, 255], 25: [255, 7, 71],
          26: [255, 9, 224], 27: [9, 7, 230], 28: [220, 220, 220], 29: [255, 9, 92], 30: [112, 9, 255],
          31: [8, 255, 214], 32: [7, 255, 224], 33: [255, 184, 6], 34: [10, 255, 71], 35: [255, 41, 10],
          36: [7, 255, 255], 37: [224, 255, 8], 38: [102, 8, 255], 39: [255, 61, 6], 40: [255, 194, 7],
          41: [255, 122, 8], 42: [0, 255, 20], 43: [255, 8, 41], 44: [255, 5, 153], 45: [6, 51, 255],
          46: [235, 12, 255], 47: [160, 150, 20], 48: [0, 163, 255], 49: [140, 140, 140], 50: [250, 10, 15],
          51: [20, 255, 0], 52: [31, 255, 0], 53: [255, 31, 0], 54: [255, 224, 0], 55: [153, 255, 0],
          56: [0, 0, 255], 57: [255, 71, 0], 58: [0, 235, 255], 59: [0, 173, 255], 60: [31, 0, 255],
          61: [11, 200, 200], 62: [255, 82, 0], 63: [0, 255, 245], 64: [0, 61, 255], 65: [0, 255, 112],
          66: [0, 255, 133], 67: [255, 0, 0], 68: [255, 163, 0], 69: [255, 102, 0], 70: [194, 255, 0],
          71: [0, 143, 255], 72: [51, 255, 0], 73: [0, 82, 255], 74: [0, 255, 41], 75: [0, 255, 173],
          76: [10, 0, 255], 77: [173, 255, 0], 78: [0, 255, 153], 79: [255, 92, 0], 80: [255, 0, 255],
          81: [255, 0, 245], 82: [255, 0, 102], 83: [255, 173, 0], 84: [255, 0, 20], 85: [255, 184, 184],
          86: [0, 31, 255], 87: [0, 255, 61], 88: [0, 71, 255], 89: [255, 0, 204], 90: [0, 255, 194],
          91: [0, 255, 82], 92: [0, 10, 255], 93: [0, 112, 255], 94: [51, 0, 255], 95: [0, 194, 255],
          96: [0, 122, 255], 97: [0, 255, 163], 98: [255, 153, 0], 99: [0, 255, 10], 100: [255, 112, 0],
          101: [143, 255, 0], 102: [82, 0, 255], 103: [163, 255, 0], 104: [255, 235, 0], 105: [8, 184, 170],
          106: [133, 0, 255], 107: [0, 255, 92], 108: [184, 0, 255], 109: [255, 0, 31], 110: [0, 184, 255],
          111: [0, 214, 255], 112: [255, 0, 112], 113: [92, 255, 0], 114: [0, 224, 255], 115: [112, 224, 255],
          116: [70, 184, 160], 117: [163, 0, 255], 118: [153, 0, 255], 119: [71, 255, 0], 120: [255, 0, 163],
          121: [255, 204, 0], 122: [255, 0, 143], 123: [0, 255, 235], 124: [133, 255, 0], 125: [255, 0, 235],
          126: [245, 0, 255], 127: [255, 0, 122], 128: [255, 245, 0], 129: [10, 190, 212], 130: [214, 255, 0],
          131: [0, 204, 255], 132: [20, 0, 255], 133: [255, 255, 0], 134: [0, 153, 255], 135: [0, 41, 255],
          136: [0, 255, 204], 137: [41, 0, 255], 138: [41, 255, 0], 139: [173, 0, 255], 140: [0, 245, 255],
          141: [71, 0, 255], 142: [122, 0, 255], 143: [0, 255, 184], 144: [0, 92, 255], 145: [184, 255, 0],
          146: [0, 133, 255], 147: [255, 214, 0], 148: [25, 194, 194], 149: [102, 255, 0], 150: [92, 0, 255],
          255: [255, 255, 255]}

# BGR for CIHP20
CIHP20 = {0: [0, 0, 0], 1: [0, 0, 128], 2: [0, 0, 255], 3: [0, 85, 0], 4: [51, 0, 170], 5: [0, 85, 255],
          6: [85, 0, 0], 7: [221, 119, 0], 8: [0, 85, 85], 9: [85, 85, 0], 10: [0, 51, 85],
          11: [128, 86, 52], 12: [0, 128, 0], 13: [255, 0, 0], 14: [221, 170, 51], 15: [255, 255, 0],
          16: [170, 255, 85], 17: [85, 255, 170], 18: [0, 255, 255], 19: [0, 170, 255]}

# BGR for MHP59
MHP59 = {0: [0, 0, 0], 1: [196, 196, 225], 2: [32, 32, 63], 3: [0, 0, 253], 4: [0, 27, 253], 5: [0, 55, 253],
         6: [0, 83, 253], 7: [0, 110, 253], 8: [0, 138, 253], 9: [0, 165, 253], 10: [0, 192, 253], 11: [0, 220, 254],
         12: [0, 248, 254], 13: [0, 255, 237], 14: [0, 255, 209], 15: [0, 255, 183], 16: [0, 255, 157],
         17: [0, 255, 131], 18: [0, 255, 104], 19: [0, 255, 80], 20: [0, 255, 57], 21: [0, 255, 39], 22: [8, 255, 32],
         23: [36, 255, 33], 24: [64, 255, 33], 25: [91, 255, 33], 26: [118, 255, 34], 27: [146, 255, 35],
         28: [173, 255, 36], 29: [200, 255, 37], 30: [228, 255, 39], 31: [255, 255, 40], 32: [255, 228, 37],
         33: [255, 199, 33], 34: [255, 171, 30], 35: [255, 144, 27], 36: [255, 114, 25], 37: [255, 85, 23],
         38: [255, 54, 21], 39: [255, 10, 20], 40: [255, 0, 20], 41: [255, 0, 30], 42: [255, 0, 51], 43: [255, 0, 76],
         44: [255, 0, 102], 45: [255, 0, 129], 46: [255, 0, 155], 47: [255, 0, 181], 48: [255, 0, 208],
         49: [255, 0, 236], 50: [246, 0, 253], 51: [219, 0, 253], 52: [191, 0, 253], 53: [164, 0, 253],
         54: [137, 0, 253], 55: [109, 0, 253], 56: [82, 0, 253], 57: [55, 0, 253], 58: [27, 0, 253]}

# BGR for CityScape35
CityScape35 = {0: [0, 0, 0], 1: [0, 0, 0], 2: [0, 0, 0], 3: [0, 0, 0], 4: [0, 0, 0], 5: [111, 74, 0],
               6: [81, 0, 81], 7: [128, 64, 128], 8: [244, 35, 232], 9: [250, 170, 160], 10: [230, 150, 140],
               11: [70, 70, 70], 12: [102, 102, 156], 13: [190, 153, 153], 14: [180, 165, 180], 15: [150, 100, 100], 
               16: [150, 120, 90], 17: [153, 153, 153], 18: [153, 153, 153], 19: [250, 170, 30], 20: [220, 220, 0], 
               21: [107, 142, 35], 22: [152, 251, 152], 23: [70, 130, 180], 24: [220, 20, 60], 25: [255, 0, 0], 
               26: [0, 0, 142], 27: [0, 0, 70], 28: [0, 60, 100], 29: [0, 0, 90], 30: [0, 0, 110], 31: [0, 80, 100], 
               32: [0, 0, 230], 33: [119, 11, 32], 34: [255, 171, 30], -1: [0, 0, 142]}


def dict_bgr2rgb(colordict):
    rgb = dict()
    keys = colordict.keys()
    for k in keys:
        rgb[k] = colordict[k][::-1]

    return rgb


def dict2array(colordict):
    keys = colordict.keys()
    colorarray = np.zeros((len(keys), 3))
    for ids, k in enumerate(keys):
        colorarray[ids] = np.asarray(colordict[k])
    colorarray = np.asarray(colorarray, dtype=np.int)

    return colorarray


def array2dict(colorarray):
    num = colorarray.shape[0]
    colordict = dict()
    for i in range(num):
        colordict[i] = list(colorarray[i])
    return colordict

